package com.fruitbasket.audioplatform;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import win.smartown.android.library.tableLayout.TableAdapter;
import win.smartown.android.library.tableLayout.TableLayout;

public class SecondFragment extends Fragment {

    //-------------------------
    private List<Content> contentList;
    private List<ContentResult> contentListResult;
    private TableLayout tableLayout;
    private TableLayout tableLayoutResult;

    Context context = ContextUtil.getContext();
    SharedPreferences sharedPreferences = null;

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //-----------------------------------------------------------------
        tableLayout = ( TableLayout )view.findViewById( R.id.main_table );
        tableLayoutResult = ( TableLayout )view.findViewById( R.id.main_table_result );
        initContent();
        initContentResult();
//        firstRowAsTitle();
        firstColumnAsTitle(tableLayout);
        firstColumnAsTitle2(tableLayoutResult);


//        sharedPreferences = context.getSharedPreferences("userData", Context.MODE_PRIVATE);
//        t1 = view.findViewById(R.id.textview_report1);
//        t1.setText(sharedPreferences.getString("report_operator_name", "defaultVal"));
//        t2 = view.findViewById(R.id.textview_report2);
//        t2.setText(sharedPreferences.getString("report_project_name", "defaultVal"));

        }

    @Override
    public void onResume() {
        Log.i(ContextUtil.LOG_TAG, "SecondFragment onResume1 ........");
        sharedPreferences = context.getSharedPreferences("userData", Context.MODE_PRIVATE);
//        t1.setText(sharedPreferences.getString("report_operator_name", "defaultVal"));
//        t2.setText(sharedPreferences.getString("report_project_name", "defaultVal"));

        Log.i(ContextUtil.LOG_TAG, "SecondFragment onResume "+ sharedPreferences.getString("report_operator_name", "defaultVal"));
        Log.i(ContextUtil.LOG_TAG, "SecondFragment onResume "+ sharedPreferences.getString("report_project_name", "defaultValxxxxxxxxxxxxxxx"));
        Log.i(ContextUtil.LOG_TAG, "SecondFragment onResume2 ........");

        super.onResume();  //在这个之前resume
    }


    //-----------------------------------------------------------------------------
    private void initContentResult() {
        contentListResult = new ArrayList<>();
        contentListResult.add(new ContentResult("粒径大小", "0-10mm", "10-20mm", "20-30mm", "300-mm") );
        contentListResult.add(new ContentResult("骨料1", "323","44","78","33") );
        contentListResult.add(new ContentResult("骨料2", "323","44","78","33") );
        contentListResult.add(new ContentResult("骨料3", "323","44","78","33") );


    }
    private void initContent() {
        contentList = new ArrayList<>();
        contentList.add(new Content("姓名:", "项目:", "时间:", "编号:"));
        contentList.add(new Content("张三", "shanghaixxxx","2021-2020-3333","xxxxx001"));


    }

    //将第一行作为标题
    private void firstRowAsTitle() {
        //fields是表格中要显示的数据对应到Content类中的成员变量名，其定义顺序要与表格中显示的相同
        final String[] fields = {"Name", "Project", "ProjectNo", "Data"};
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return fields.length;
            }

            @Override
            public String[] getColumnContent(int position) {
                int rowCount = contentList.size();
                String contents[] = new String[rowCount];
                try {
                    Field field = Content.class.getDeclaredField(fields[position]);
                    field.setAccessible(true);
                    for (int i = 0; i < rowCount; i++) {
                        contents[i] = (String) field.get(contentList.get(i));
                    }
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                return contents;
            }
        });
    }

    //将第一列作为标题
    private void firstColumnAsTitle(TableLayout tableLayout ) {
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return contentList.size();
            }

            @Override
            public String[] getColumnContent(int position) {
                return contentList.get(position).toArray();
            }
        });
    }
    //将第一列作为标题
    private void firstColumnAsTitle2(TableLayout tableLayout ) {
        tableLayout.setAdapter(new TableAdapter() {
            @Override
            public int getColumnCount() {
                return contentListResult.size();
            }

            @Override
            public String[] getColumnContent(int position) {
                return contentListResult.get(position).toArray();
            }
        });
    }

    private String newRandomNumber() {
        return (new Random().nextInt(50) + 50) + "";
    }

    public static class Content {
        private String a;
        private String b;
        private String c;
        private String d;

        public Content( String Name, String Project, String ProjectNo, String ProjectDate ) {
            this.a = Name;
            this.b = Project;
            this.c = ProjectNo;
            this.d = ProjectDate;

        }
        public String[] toArray() {
            return new String[]{ a,b,c,d };
        }

    }

    public static class ContentResult {
        private String a;
        private String b;
        private String c;
        private String d;
        private String e;

        public ContentResult( String Name, String scale1, String scale2, String scale3, String scale4 ) {
            this.a = Name;
            this.b = scale1;
            this.c = scale2;
            this.d = scale3;
            this.e = scale4;
        }
        public String[] toArray() {
            return new String[]{ a,b,c,d,e };
        }

    }
}